package com.yss.reportworld.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.yss.common.core.utils.easyexecl.EasyExeclUtil;
import com.yss.reportworld.domain.TableDetail;
import com.yss.reportworld.service.ITableDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.yss.common.logger.annotation.Log;
import com.yss.common.logger.enums.BusinessType;
import com.yss.common.security.annotation.RequiresPermissions;
import com.yss.reportworld.domain.TableIndexed;
import com.yss.reportworld.service.ITableIndexedService;
import com.yss.common.core.web.controller.BaseController;
import com.yss.common.core.web.domain.AjaxResult;
import com.yss.common.core.web.page.TableDataInfo;

/**
 * 索引信息Controller
 *
 * @author yss
 * @date 2023-03-31
 */
@RestController
@RequestMapping("/indexed")
public class TableIndexedController extends BaseController {
    @Autowired
    private ITableIndexedService tableIndexedService;
    @Autowired
    private ITableDetailService tableDetailService;

    /**
     * 查询索引信息列表
     */
    @RequiresPermissions("reportworld:indexed:list")
    @GetMapping("/list")
    public TableDataInfo list(TableIndexed tableIndexed) {
        startPage();
        List<TableIndexed> list = tableIndexedService.selectTableIndexedList(tableIndexed);
        return getDataTable(list);
    }

    /**
     * 初始化 加载
     */
    @RequiresPermissions("reportworld:indexed:list")
    @Log(title = "索引信息", businessType = BusinessType.OTHER)
    @PostMapping("/init")
    public AjaxResult init(@RequestBody TableIndexed tableIndexed) {
        AjaxResult ajax = AjaxResult.success();
        TableDetail tableDetail = new TableDetail();
        tableDetail.setTableId(tableIndexed.getTableId());
        tableDetail.setHisVersion(tableIndexed.getHisVersion());
        ajax.put("tableColumns", tableDetailService.selectTableDetailList(tableDetail, false));
        return ajax;
    }


    /**
     * 导出索引信息列表
     */
    @RequiresPermissions("reportworld:indexed:export")
    @Log(title = "索引信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TableIndexed tableIndexed) {
        List<TableIndexed> list = tableIndexedService.selectTableIndexedList(tableIndexed);
        EasyExeclUtil.export(response, list, TableIndexed.class);
    }

    /**
     * 获取索引信息详细信息
     */
    @RequiresPermissions("reportworld:indexed:query")
    @PostMapping(value = "/getIndexed")
    public AjaxResult getInfo(@RequestBody TableIndexed tableIndexed) {
        return success(tableIndexedService.selectTableIndexedById(tableIndexed));
    }

    /**
     * 新增索引信息
     */
    @RequiresPermissions("reportworld:indexed:add")
    @Log(title = "索引信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody @Validated TableIndexed tableIndexed) {
        if (!tableIndexedService.checkTableIndexedUnique(tableIndexed)) {
            return error("索引【" + tableIndexed.getIndexedName() + "】字段名称【" + tableIndexed.getIndexedColumnName() + "】已存在");
        }
        AjaxResult ajax = AjaxResult.success();
        tableIndexedService.addTableIndexed(tableIndexed);
        return ajax;
    }

    /**
     * 修改索引信息
     */
    @RequiresPermissions("reportworld:indexed:edit")
    @Log(title = "索引信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody @Validated TableIndexed tableIndexed) {
        if (!tableIndexedService.checkTableIndexedUnique(tableIndexed)) {
            return error("索引【" + tableIndexed.getIndexedName() + "】字段名称【" + tableIndexed.getIndexedColumnName() + "】已存在");
        }
        AjaxResult ajax = AjaxResult.success();
        tableIndexedService.etidTableIndexed(tableIndexed);
        return ajax;
    }

    /**
     * 删除索引信息
     */
    @RequiresPermissions("reportworld:indexed:remove")
    @Log(title = "索引信息", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    public AjaxResult remove(@RequestBody TableIndexed tableIndexed) {
        tableIndexedService.delTableIndexed(tableIndexed);
        return success();
    }
}
